#pragma once
#include "reactor.hpp"
#include "Util.hpp"
#include <sys/socket.h>
#include <sys/types.h>
#include <vector>
#include <errno.h>

#define ONCE_SIZE 128
//1: 读取数据成功 -1:读取失败 0:连接关闭了
int recvcore(int sock, std::string& outbuffer)
{
    while(true)
    {
        char buffer[ONCE_SIZE];
        ssize_t s = recv(sock, buffer, ONCE_SIZE-1, 0);
        if(s > 0)
        {
            buffer[s] = 0;
            outbuffer += buffer;
            std::cout << "到来的数据 " << outbuffer << std::endl;
        }
        else if(s < 0)
        {
            //IO被信号打断
            if(errno == EINTR)
                continue;

            //底层没数据了
            if(errno == EAGAIN || errno == EWOULDBLOCK)
                return 1;

            return -1; //真的出错了
        }
        else
        {
            return 0;
        }
    }
}

int recver(Event* ev)
{
    std::cout << "有数据到来啦" << std::endl;
    //1.读取数据
    int result = recvcore(ev->sock, ev->inbuffer);
    if(result <= 0)
    {
        if(ev->errorer)
            ev->errorer(ev);

        return -1;
    }
    //2.分包
    std::string sep = "X";
    std::vector<std::string> ret;
    SplitSegment(ret, ev->inbuffer, sep);
    //3.反序列化
    int sz = ret.size();
    for(int i = 0;i < sz;++i)
    {
        std::string data1, data2;
        if(Deserialize(data1, data2, ret[i]))
        {
            int d1 = atoi(data1.c_str());
            int d2 = atoi(data2.c_str());
            int result = d1 + d2;
            //4.构造响应
            std::string res = data1;
            res += "+";
            res += data2;
            res += "=";
            res += std::to_string(result);
            res += sep;

            ev->outbuffer += res; //发送数据
            std::cout << "ev->outbuffer : " << ev->outbuffer << std::endl;
        }
        
    }
    //5.发送数据
    if(!(ev->outbuffer).empty())
        ev->R->EnableRW(ev->sock, true, true);

    return 0;
}

int sendcore(int sock, std::string& buffer)
{
    int total = 0;
    int sz = buffer.length();
    const char* start = buffer.c_str();
    while(true)
    {
        ssize_t s = send(sock, start + total, sz-total, 0);
        std::cout << "写数据： " << s << std::endl;
        if(s > 0)
        {
            total += s;
            if(total == sz)
            {
                buffer.clear(); //数据已读完
                return 1;
            }
        }
        else
        {
            //被信号中断了
            if(errno == EINTR)
                continue;
            
            //无法再发了
            if(errno == EAGAIN || errno == EWOULDBLOCK)
            {
                buffer.erase(0, total);
                return 0;
            }

            return -1;
        }
    }
    
}

int sender(Event* ev)
{
    std::cout << "有数据需要发送啦" << std::endl;
    //1.发送数据
    int result = sendcore(ev->sock, ev->outbuffer);
    if(result > 0)
    {
        ev->R->EnableRW(ev->sock, true, false);
    }
    else if(result == 0)
    {
        ev->R->EnableRW(ev->sock, true, true);
    }
    else
    {
        if(ev->errorer)
            ev->errorer(ev);
    }

    return 0;
}

int errorer(Event* ev)
{
    std::cout << "发生了异常" << std::endl;
    ev->R->DeleteEvent(ev);

    return 0;
}

